package com.starbaba.system.user.dao;

import java.util.Date;
import java.util.List;

import org.springframework.util.Assert;

import com.starbaba.core.hibernate.DAOFactory;
import com.starbaba.core.hibernate.HibernateDAO;
import com.starbaba.core.hibernate.Strategy;
import com.starbaba.core.web.constant.WebConstants;
import com.starbaba.system.user.model.UserVO;

@SuppressWarnings({"serial","unchecked"})
public class UserLoseStrategy implements Strategy {
	
	
	private UserVO userVO;
	private String userid;
	private String opertype;
	
	public UserLoseStrategy() {
		super();
	}
	
	public UserLoseStrategy(UserVO userVO, String userid, String opertype) {
		super();
		this.userVO = userVO;
		this.userid = userid;
		this.opertype = opertype;
	}
	
	@Override
	public Object process() throws Exception {
		HibernateDAO userDAO = DAOFactory.getInstance().createHibernateDAO(UserVO.class);
		String newUserStatus = WebConstants.STATUS_INVALID;
		if("LOSE".equals(opertype)){
			newUserStatus = WebConstants.STATUS_INVALID;
		} else {
			throw new Exception("操作类型不合法.opertype:"+opertype);
		}
		
		UserVO userVO = (UserVO)userDAO.findByPK(this.userid);
		Assert.notNull(userVO, "用户不存在.用户ID:"+userid);
		
		userVO.setStatus(newUserStatus);
		userVO.setLasteditor(this.userVO.getUserid());
		userVO.setLasttime(new Date());
		userDAO.saveOrUpdate(userVO);
			
		List<UserVO> userList = (List<UserVO>)userDAO.findByProperty("userid", this.getUserid());
		for(UserVO userNewVO : userList){
			userNewVO.setStatus(newUserStatus);
			userDAO.saveOrUpdate(userNewVO);
		}
		return true;
	}
	public UserVO getUserVO() {
		return userVO;
	}
	public void setUserVO(UserVO userVO) {
		this.userVO = userVO;
	}
	public String getUserid() {
		return userid;
	}
	public void setUserid(String userid) {
		this.userid = userid;
	}
	public String getOpertype() {
		return opertype;
	}
	public void setOpertype(String opertype) {
		this.opertype = opertype;
	}


}
